package com.wdb007.venus.wss.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wdb007.venus.svc.facade.BookShelfService;
import com.wdb007.venus.wss.channelgroup.BookCaseChannelGroups;
import com.wdb007.venus.wss.service.WebSocketServerService;

import io.netty.channel.ChannelHandlerContext;

@Service("bookCaseHBService")
public class BookCaseHBService implements WebSocketServerService {

	private static Logger logger = LoggerFactory.getLogger(BookCaseHBService.class);
	@Autowired
	private BookShelfService bookShelfService;
	@Override
	public String doService(ChannelHandlerContext ctx, String request) throws Exception {
		JSONObject jsonObj = JSON.parseObject(request);
		logger.info("BookCaseReq 接收websocket响应信息: {}", jsonObj.toJSONString());
		
		JSONObject response = new JSONObject();
		response.put("action", "hbres");
		response.put("id", jsonObj.get("id"));
		response.put("ts", System.currentTimeMillis());
		try {
			bookShelfService.heartBeat(jsonObj);
		}catch(Exception e) {
			logger.error("心跳报错", e);
		}
		
		BookCaseChannelGroups.broadcast(jsonObj.getString("id"), response.toJSONString());
		return null;
	}

}
